package ebcs.scan.controller.groupCtr;

import com.google.gson.Gson;

import ebcs.database.bean.BsGroupInfor;
import ebcs.database.bean.BsProTeamDetailInfo;
import ebcs.database.bean.BsUserDetailInfo;
import ebcs.database.bean.BsUserInfor;
import ebcs.database.bean.RpUserTaskNeedBean;
import ebcs.database.bean.SysFunction;
import ebcs.database.service.BsGroupInforServiceImp;
import ebcs.database.service.SystemServiceImp;
import ebcs.database.service.impl.BsProTeamServiceImpl;
import ebcs.database.service.impl.BsUserInforServiceImpl;
import ebcs.database.service.impl.BsUserNeedServiceImpl;
import ebcs.scan.common.consts.KLinkUsSysConsts;
import ebcs.scan.common.consts.UserStatus;
import ebcs.scan.common.utils.HttpClientUtil;
import ebcs.scan.common.utils.Properties;
import ebcs.scan.service.wechat.WechatCtrlService;
import jplat.core.session.JSession;
import jplat.core.trans.JAppBaseService;
import jplat.core.trans.JAppContext;
import jplat.core.trans.JAppContextFactory;
import jplat.error.exception.JSystemException;
import jplat.error.exception.JTransException;
import jplat.tools.config.JAppConfig;
import jplat.tools.string.DateUtil;
import jplat.tools.string.JRandomUtil;
import jplat.tools.string.StringUtil;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 团队信息管理
 * @author Administrator
 *
 */
@Controller
public class GroupManagerCtrl extends JAppBaseService {
    private Logger logger = Logger.getLogger(GroupManagerCtrl.class);


    protected JAppContextFactory serviceFactory = new JAppContextFactory();


    //数据库库服务.


    @Autowired
    public BsGroupInforServiceImp groupService;

    @Autowired
    public SystemServiceImp systemService;
    @Autowired
    public WechatCtrlService wechatService;

    public static String CHARSET_ENCODING = "UTF-8";

   

    //团队后台管理
    //1、团队信息查询
    @RequestMapping("/page/sys/searchGroupInfor.do")
    public void searchGroupInfor(HttpServletRequest request, HttpServletResponse response) throws JTransException, JSystemException {
        JAppContext context = serviceFactory.buildAppContextNoUser(request, response);//自动客户信息
        BsGroupInfor group = context.obtainReqModel(BsGroupInfor.class);
        String endRow = ((Integer.parseInt(group.getStartRow()))+(Integer.parseInt(group.getPageSize())))+"";
        group.setEndRow(endRow);
        List<BsGroupInfor> groupDetailInfoList = groupService.selectGroupDetailPage(group);
        int count=groupService.selectGroupDetailCount(group);
        Map rsModel = new HashMap();
		rsModel.put("count", count);
  		rsModel.put("groupList", groupDetailInfoList);
 		context.buildAppResponse(rsModel);
    }
    //2、团队信息查询返回更新页面
    @RequestMapping("/page/sys/{proGroupNo}/selectSingleGroup.do")
    public String selectSingleGroup(HttpServletRequest request, HttpServletResponse response, @PathVariable String proGroupNo) throws JTransException, JSystemException {
    	BsGroupInfor group = new BsGroupInfor();
    	group.setProGroupNo(proGroupNo);
        List<BsGroupInfor> groupDetailInfoList = groupService.selectGroupDetail(group);
        if (!CollectionUtils.isEmpty(groupDetailInfoList)) {
            request.setAttribute("group", groupDetailInfoList.get(0));
        }
        return "main/report/updateGroup";
    }
    //3、删除团队信息
    @RequestMapping("/page/sys/deleteSingleGroup.do")
    public void deleteSingleGroup(HttpServletRequest request, HttpServletResponse response) throws JTransException, JSystemException {
        JAppContext context = serviceFactory.buildAppContextNoUser(request, response);//自动客户信息
        BsGroupInfor group = context.obtainReqModel(BsGroupInfor.class);
        groupService.deleteGroupByGroupNo(group.getProGroupNo());
        Map rspModel = new HashMap();
        context.buildAppResponse(rspModel);
    }
  //4、添加团队信息
    @RequestMapping("/page/sys/addGroupInfor.do")
    public void addGroupInfor(HttpServletRequest request, HttpServletResponse response) throws JTransException, JSystemException {
        JAppContext context = serviceFactory.buildAppContextNoUser(request, response);//自动客户信息
        BsGroupInfor group = context.obtainReqModel(BsGroupInfor.class);
        if(StringUtils.isNotBlank(group.getProGroupName())&&StringUtils.isNotBlank(group.getGroupManger())){
        	List<BsGroupInfor> groupInforList = this.groupService.selectGroupByGroupName(group.getProGroupName());
            if (!CollectionUtils.isEmpty(groupInforList)) {
                for (BsGroupInfor bsGroupInfor : groupInforList) {
                    if (bsGroupInfor.getProGroupNo().equals(group.getProGroupNo())) {
                        continue;
                    }
                    throw new JTransException("1001", "团队名称重复了！");
                }
            }	
        }
    	group.setCreateDate(DateUtil.getSystemTimeBySomeFormat("yyyyMMdd"));
    	group.setCreateTime(DateUtil.getTime());
    	String rouid = this.groupService.selectGroupMaxRouId();
    	logger.info("groupMaxRouid:" + rouid);
    	group.setRouid(rouid);
    	group.setProGroupNo("G"+rouid);
    	groupService.insertGroupInfor(group);
        Map rspModel = new HashMap();
        context.buildAppResponse(rspModel);
    }
    @RequestMapping("/page/sys/updateGroup.do")
    public void updateGroup(HttpServletRequest request, HttpServletResponse response) throws JTransException, JSystemException {
        JAppContext context = serviceFactory.buildAppContextNoUser(request, response);//自动客户信息
        BsGroupInfor group = context.obtainReqModel(BsGroupInfor.class);
        if (StringUtils.isNotBlank(group.getProGroupNo())&&StringUtils.isNotBlank(group.getProGroupName())) {
            List<BsGroupInfor> groupInforList = this.groupService.selectGroupByGroupName(group.getProGroupName());
            if (!CollectionUtils.isEmpty(groupInforList)) {
                for (BsGroupInfor bsGroupInfor : groupInforList) {
                    if (bsGroupInfor.getProGroupNo().equals(group.getProGroupNo())) {
                        continue;
                    }
                    throw new JTransException("1001", "团队名称重复了！");
                }
            }
        }
        int i = this.groupService.updateGroupByGroupNo(group);
        if (i == 0) {
            throw new JTransException("1001", "新增团队信息失败了。");
        }
        Map rspModel = new HashMap();
        context.buildAppResponse(rspModel);
    }

}
